package org.shangrila.bme.mb.mobile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.app.wechat.utils.AccessWxUtil;
import org.shangrila.app.wechat.utils.WeChatUtil;
import org.shangrila.app.wfm.mb.WfmTaskMgrMB;
import org.shangrila.bme.eo.BmeReceipt;
import org.shangrila.bme.eo.BmeReceiptDetail;
import org.shangrila.bme.eo.BmeWfmService;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.wfm.core.WfmEngine;
import org.shangrila.wfm.core.WfmHelper;
import org.shangrila.wfm.eo.WfmTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class MobileReceiptMB implements Serializable {
	private static final long serialVersionUID = -512146464217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	private List<WfmTask> voTasks = new ArrayList<WfmTask>();
	private List<BmeReceipt> voList = new ArrayList<BmeReceipt>();
	private WfmTask selectedTask;
	private int rowNum = 0;
	private BmeReceipt selectedReceipt;	

	private List<BmeReceiptDetail> vod = new ArrayList<BmeReceiptDetail>(); //详情

	private boolean signFlag = true;
	private boolean taskFlag = false;
	private String operator;
	private WfmEngine engine;
	WfmTaskMgrMB wfmTaskMgrMB;
	private String taskName="wf_fhd_sign";
	@Inject
	private DbAccess db;
	
	private String queryPage = "/pages/mobile/mb_receipt_sign_query.xhtml";

	@PostConstruct
    public void init() {
		engine = WfmHelper.getEngine();
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="wfm";
		}
		queryTasks();
    }

	public String queryTasks() {

		voTasks = engine.getWfmdao().findTaskByName(taskName);
		log.info("QueryTask="+voTasks.size());
		voList = new ArrayList<BmeReceipt>();
		for(WfmTask wt:voTasks) {
			List<BmeWfmService> bwsList = db.findBySql("select m from BmeWfmService m where m.businessType='3' and m.serviceNo='"+wt.getServiceNo()+"'");
			if(bwsList!=null && bwsList.size()>0) {
				BmeWfmService bws = bwsList.get(0);
				List<BmeReceipt> list = db.findBySql("select m from BmeReceipt m where m.id='"+bws.getBusinessId()+"'");
				if(list!=null && list.size()>0) {
					
					BmeReceipt bmeReceipt = list.get(0);
					voList.add(bmeReceipt);
				}
				
			}
		}
		log.info("Queryreceipt="+voList.size());
		return null;
	}
	
	public String nullAction() {
		return null;
	}

	public String signAction() {
		for(BmeReceiptDetail bd:selectedReceipt.getBmeReceiptDetails()) {
			db.begin();
			bd.setSignTime(new Date());
			db.update(bd);
			db.commit();
		}
		selectedReceipt.setStatus("0");
		db.begin();
		db.update(selectedReceipt);
		db.commit();
		
		signFlag = false;
		taskFlag = true;
		WeChatUtil.sendTextMessage(AccessWxUtil.wxUserId,"收货单号："+selectedReceipt.getId()+"\n发货单签收成功！");
		AppInitInfo.showMessage("发货单签收", "处理成功");
		return null;
	}
	
	public String commitTask() {
		log.info("-----"+rowNum);
		selectedTask = voTasks.get(rowNum);
		
		if(selectedTask!=null) {
			selectedTask.setResult(0);
			engine.commitTask(selectedTask, operator, null);
			voTasks.remove(selectedTask);
			voList.remove(selectedReceipt);
		}
		WeChatUtil.sendTextMessage(AccessWxUtil.wxUserId,"发货单流程提交成功！");
		AppInitInfo.showMessage("流程提交", "处理成功");
		return queryPage;
	}
	
	public List<BmeReceipt> getVoList() {
		return voList;
	}

	public void setVoList(List<BmeReceipt> voList) {
		this.voList = voList;
	}

	public WfmTask getSelectedTask() {
		return selectedTask;
	}

	public void setSelectedTask(WfmTask selectedTask) {
		this.selectedTask = selectedTask;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public BmeReceipt getSelectedReceipt() {
		return selectedReceipt;
	}

	public void setSelectedReceipt(BmeReceipt selectedReceipt) {
		this.selectedReceipt = selectedReceipt;
		if(selectedReceipt.getStatus().equals("0")){
			signFlag = false;
			taskFlag = true;
		}else {
			signFlag = true;
			taskFlag = false;
		}
	}

	public List<BmeReceiptDetail> getVod() {
		return vod;
	}

	public void setVod(List<BmeReceiptDetail> vod) {
		this.vod = vod;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public boolean isSignFlag() {
		return signFlag;
	}

	public void setSignFlag(boolean signFlag) {
		this.signFlag = signFlag;
	}

	public boolean isTaskFlag() {
		return taskFlag;
	}

	public void setTaskFlag(boolean taskFlag) {
		this.taskFlag = taskFlag;
	}

}